package org.apache.ibatis.binding;

import static org.junit.Assert.assertEquals;

import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.domain.blog.Author;
import org.apache.ibatis.domain.blog.Blog;
import org.apache.ibatis.domain.blog.Post;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.BeforeClass;
import org.junit.Test;

public class BindingTestMY {
	private static SqlSessionFactory sqlSessionFactory;

	/**
	 * 前置方法
	 * @throws Exception
	 */
	@BeforeClass
	public static void setup() throws Exception {
		DataSource dataSource = BaseDataTest.createBlogDataSource();
		BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL);
		BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA);
		TransactionFactory transactionFactory = new JdbcTransactionFactory();
		Environment environment = new Environment("Production", transactionFactory, dataSource);
		Configuration configuration = new Configuration(environment);
		configuration.setLazyLoadingEnabled(true);
		configuration.getTypeAliasRegistry().registerAlias(Blog.class);
		configuration.getTypeAliasRegistry().registerAlias(Post.class);
		configuration.getTypeAliasRegistry().registerAlias(Author.class);
		configuration.addMapper(BoundBlogMapper.class);
		configuration.addMapper(BoundBlogMapperMY.class);
		configuration.addMapper(BoundAuthorMapper.class);
		configuration.addMapper(BoundAuthorMapperMY.class);
		
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
	}

	/**
	 * 不知道"RowBounds"的功用，特地验证下。
	 * @throws Exception
	 */
	@Test
	public void shouldfindThreeSpecificPosts() throws Exception {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			BoundAuthorMapperMY mapper = session.getMapper(BoundAuthorMapperMY.class);
			//List<Post> posts = mapper.findThreeSpecificPosts(1, new RowBounds(1, 1), 3, 5);
			//List<Post> posts = mapper.findThreeSpecificPosts(1, 3, 5);
			List<Post> posts = mapper.findThreeSpecificPosts(new RowBounds(1, 1),1,  3, 5);
			assertEquals(1, posts.size());
			/**
			 * new RowBounds(0, 1) -> 1
			 * new RowBounds(1, 1) -> 3
			 * new RowBounds(2, 1) -> 5
			 */
			assertEquals(3, posts.get(0).getId());
			session.rollback();
		} finally {
			session.close();
		}
	}
	
	//@Test(expected = Exception.class)
	@Test
	public void shouldFailWhenSelectingOneBlogWithNonExistentParam() {
		SqlSession session = sqlSessionFactory.openSession();
		try {
			BoundBlogMapperMY mapper = session.getMapper(BoundBlogMapperMY.class);
			mapper.selectBlogByNonExistentParam(1);
		} finally {
			session.close();
		}
	}
	
	

}
